package cn.itcast.bos.web.action.take_delivery;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.promotion.PromotionService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class promotionAction extends BaseAction<Promotion>{
	@Autowired
	private PromotionService promotionService;
	
	private File titleImgFile;
	private String titleImgFileFileName;

	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}

	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}

	
	@Action(value = "promotion_save", results = { @Result(name = "success", type = "redirect",location="./pages/take_delivery/promotion.html") })
	public String save() throws IOException{
		String savePath = ServletActionContext.getServletContext().getRealPath(
				"/upload/");
		//生成随机的图片名称
		UUID uuid = UUID.randomUUID();
		String sum = titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
		String uuidrandomName=uuid+sum;
		File destFile = new File(savePath+"/"+uuidrandomName);
		System.out.println(destFile);
		//将我们的图片保存在destFile路径下
		FileUtils.copyFile(titleImgFile, destFile);
		model.setTitleImg(ServletActionContext.getRequest().getContextPath()
				+ "/upload/" + uuidrandomName);
		//交个service
		promotionService.save(model);
		return SUCCESS;
	}
	private Integer page;
	private Integer rows;
	
	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	@Action(value = "promotion_pageQuery", results = { @Result(name = "success", type = "json")})
	public String findAllPromotion(){
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Promotion> pagelist = promotionService.findpageData(pageable);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("totle", pagelist.getTotalElements());
		result.put("rows", pagelist.getContent());
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
